package com.shijie.service;

import com.shijie.core.AbstractService;
import com.shijie.core.ServiceException;
import com.shijie.domain.entity.Activitys;
import com.shijie.domain.entity.Products;
import com.shijie.domain.entity.ShoppingTrolley;
import com.shijie.domain.entity.Skus;
import com.shijie.mapper.ActivitysMapper;
import com.shijie.mapper.ProductsMapper;
import com.shijie.mapper.ShoppingTrolleyMapper;
import com.shijie.mapper.SkusMapper;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
 *  @author: smart boy
 *  @date: 2018-08-16
 */
@Service
public class ShoppingTrolleyService extends AbstractService<ShoppingTrolley> {

	@Resource
	private ShoppingTrolleyMapper shoppingTrolleyMapper;

	@Resource
	private ProductsMapper productsMapper;

	@Resource
	private SkusMapper skusMapper;

	@Resource
	private ActivitysMapper activitysMapper;

	/**
	 * 查询用户购物车情况
	 *
	 * @param skuIds
	 * @return
	 */
	public List<Skus> listByIds(String[] skuIds) {
		List<Skus> list = new ArrayList<>();
		for (String skuId : skuIds) {
			Skus sku = skusMapper.selectByPrimaryKey(Integer.parseInt(skuId));
			if (sku != null) {
				// TODO 因为现在的活动优惠策略没有，优惠后价格等于原价
				sku.setSalePrice(sku.getPrice());
				// 获取该skus最近参加的活动
				List<Activitys> activitys = activitysMapper.listByProductId(sku.getProductId());
				if (activitys != null && activitys.size() > 0) {
					sku.setActivity(activitys.get(0));
				}
				list.add(sku);
			}

		}
		return list;
	}

	/**
	 * 查询用户购物车情况
	 *
	 * @param userId
	 * @return
	 */
	public List<ShoppingTrolley> listByUserId(Integer userId) {
		Example con = new Example(ShoppingTrolley.class);
		Example.Criteria criteria = con.createCriteria();

		criteria.andEqualTo("userId", userId);
		con.orderBy("id").asc();

		List<ShoppingTrolley> list = shoppingTrolleyMapper.selectByCondition(con);

		for (ShoppingTrolley shoppingTrolley : list) {
			shoppingTrolley.setProduct(productsMapper.selectByPrimaryKey(shoppingTrolley.getProductId()));
			Skus sku = skusMapper.selectByPrimaryKey(shoppingTrolley.getSkuId());
			shoppingTrolley.setSku(sku);
			shoppingTrolley.setIsEnough(sku.getInvestory() >= shoppingTrolley.getNum());
		}

		return list;
	}

	public void save(ShoppingTrolley shoppingTrolley) throws ServiceException {
		// 如果ID 不为空，认为是在购物车界面编辑数量，否则认为是商品详情页，加入购物车
		if (shoppingTrolley.getId() != null) {
			ShoppingTrolley oriShoppingTrolley = shoppingTrolleyMapper.selectByPrimaryKey(shoppingTrolley.getId());
			if (checkInvestory(shoppingTrolley.getNum(), oriShoppingTrolley.getSkuId())) {
				throw new ServiceException("加入购物车失败，库存不够！");
			}

			oriShoppingTrolley.setNum(shoppingTrolley.getNum());
			oriShoppingTrolley.setUpdateTime(new Date());
			shoppingTrolleyMapper.updateByPrimaryKey(oriShoppingTrolley);
		} else {
			if (checkInvestory(shoppingTrolley.getNum(), shoppingTrolley.getSkuId())) {
				throw new ServiceException("加入购物车失败，库存不够！");
			}

			Example con = new Example(ShoppingTrolley.class);
			Example.Criteria criteria = con.createCriteria();
			criteria.andEqualTo("userId", shoppingTrolley.getUserId())
					.andEqualTo("skuId", shoppingTrolley.getSkuId())
					.andEqualTo("productId", shoppingTrolley.getProductId());

			List<ShoppingTrolley> list = shoppingTrolleyMapper.selectByCondition(con);
			if (list == null || list.size() == 0) {
				// insert
				shoppingTrolley.setCreateTime(new Date());
				shoppingTrolleyMapper.insert(shoppingTrolley);
			} else {
				// update
				ShoppingTrolley oriShoppingTrolley = list.get(0);
				oriShoppingTrolley.setNum(shoppingTrolley.getNum());
				oriShoppingTrolley.setUpdateTime(new Date());
				shoppingTrolleyMapper.updateByPrimaryKey(oriShoppingTrolley);
			}
		}

	}


	/**
	 * 校验库存
	 *
	 * @param updateNum 购物车商品的数量
	 * @param skuId skuId
	 * @return
	 */
	private Boolean checkInvestory(Integer updateNum,Integer skuId) {

		Skus sku = skusMapper.selectByPrimaryKey(skuId);
		if (updateNum > sku.getInvestory()) {
			return Boolean.FALSE;
		} else {
			return Boolean.TRUE;
		}


	}
}
